package cn.itsource.rpms.web.controller;

import cn.itsource.rpms.domain.Setted;
import cn.itsource.rpms.query.BaseQuery;
import cn.itsource.rpms.query.SettedQuery;
import cn.itsource.rpms.service.ISettedService;
import cn.itsource.rpms.util.AjaxResult;
import cn.itsource.rpms.util.CopyBean;
import cn.itsource.rpms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/setted")
@CrossOrigin
public class SettedController {

    @Autowired
    private ISettedService service;

    @RequestMapping(value="/queryAll",method = RequestMethod.PATCH)
    @ResponseBody
    public List<Setted> queryAll(){
        return service.queryAll();
    }
    @RequestMapping(value="/queryCar",method = RequestMethod.PATCH)
    @ResponseBody
    public PageResult<Setted> queryCar(@RequestBody SettedQuery query){
        return service.queryCar(query);
    }

    @RequestMapping(value="/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Setted queryOne(@PathVariable Long id){
        return service.queryOne(id);
    }

    @RequestMapping(value="/add",method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult add(@RequestBody Setted setted){
        try {
            service.add(setted);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @RequestMapping(value="/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResult delete(@PathVariable Long id){
        try {
            service.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());

        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody Setted setted){
        try {

            System.out.println(setted);
            Setted one = service.queryOne(setted.getId());
            Date settedTime = one.getSettedTime();
            System.out.println(settedTime);
            CopyBean.copyNotNullFiled(setted,one);
            one.setSettedTime(settedTime);
            System.out.println(one);
            service.update(one);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }

    }

    @RequestMapping(value = "/page",method = RequestMethod.PATCH)
    @ResponseBody
    public PageResult<Setted> pageList(@RequestBody SettedQuery query){
        return service.queryPage(query);
    }


    @RequestMapping(value = "/batchDelete",method = RequestMethod.PATCH)
    @ResponseBody
    public AjaxResult batchDelete(Long[] ids){
        try {
            List<Long> idList = Arrays.asList(ids);
            service.batchDelete(idList);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

}
